package com.bfly.mall.product.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.bfly.common.json.JsonUtil;
import com.bfly.core.base.entity.BaseEntity;
import com.bfly.mall.product.dto.SpeItemDTO;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 产品多规格属性
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/15 21:23
 */
@TableName(value = "product_spe_attr", resultMap = "ProductSpeAttrResultMap")
public class ProductSpeAttr extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 7279569423788951936L;

    @TableField("product_id")
    private int productId;

    /**
     * 属性名称
     */
    private String name;

    /**
     * 属性值 JSON字符串
     *
     * @see SpeItemDTO
     */
    private String data;

    private int seq;

    /**
     * 字符串转换为实体类对象集合
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/22 20:30
     */
    public List<SpeItemDTO> getItems() {
        if (StringUtils.isBlank(getData())) {
            return new ArrayList<>();
        }
        List<SpeItemDTO> items = JsonUtil.parseStrToList(getData(), SpeItemDTO.class);
        if(items!=null) {
            Collections.sort(items);
        }
        return items;
    }

    public int getSeq() {
        return seq;
    }

    public void setSeq(int seq) {
        this.seq = seq;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
